﻿<#include "include/_meta.html">
<title>我要还车</title>
</head>
<body>

<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 我要租车 <span
            class="c-gray en">&gt;</span> 订单查询 <a class="btn btn-success radius r"
                                                  style="line-height:1.6em;margin-top:3px"
                                                  href="javascript:location.replace(location.href);" title="刷新"><i
                class="Hui-iconfont">&#xe68f;</i></a></nav>
<div class="page-container">
    <div class="text-c">
        订单编号：<input type="text" class="input-text" style="width:250px" placeholder="输入订单编号" id="orderId" name="orderid">
        客户ID：<input type="text" class="input-text" style="width:250px" placeholder="输入客户ID" id="customerId"
                    name="mobile">
        <button type="submit" class="btn btn-success" id="searchOrders" name=""><i class="Hui-iconfont">&#xe665;</i> 搜订单
        </button>
        <button type="button" class="btn btn-success" id="ZHIFU" name=""><i class="Hui-iconfont">&#xe665;</i> 查询交易
        </button>
    </div>
    <table id="orderListTable" class="table table-border table-bordered table-bg table-hover" style="margin: 0">
        <thead>
        <tr>
            <th scope="col" colspan="11" style="text-align: center">订单列表</th>
        </tr>
        <tr class="text-c">
            <th>订单号</th>
            <th>订单状态</th>
            <th>汽车ID</th>
            <th>客户ID</th>
            <th>预约取车日期</th>
            <th>预约还车日期</th>
            <th>取车门店</th>
            <th>还车门店</th>
            <th>订单创建时间</th>
            <th>订单结束时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
</div>

<#include "include/_footer.html">

<script type="text/javascript">
    $(function () {
        initTable()

    })

    $("#ZHIFU").click(function () {
        layer.open({
            type: 2,
            title: "查询交易",
            fix: false,
            maxmin: true,
            shadeClose: true,
            area: ['1000px', '800px'],
            content: '/toALIPay',
            end: function () {
                initTable();
            }
        })
    })

    function initTable() {
        $("#orderListTable").dataTable({
            "bFilter": false, // 去掉搜索框
            "sPaginationType": "full_numbers",
            "bDestroy": true,
            "bProcessing": true,
            "bAutoWidth": false,
            "bServerSide": true, //服务器分页
            "bSort": false,
            "aLengthMenu": [[5, 10, 25, 50], [5, 10, 25, 50]],
            "oLanguage": {
                "sProcessing": " 订单数据正在加载中 ......",
                "sLengthMenu": " 每页显示  _MENU_  条记录 ",
                "sZeroRecords": " 没有数据！ ",
                "sEmptyTable": " 没有找到相关数据！ ",
                "sInfo": " 当前显示  _START_  到  _END_  条，共  _TOTAL_  条记录 ",
                "sInfoEmpty": " 显示 0 到 0 条记录 ",
                "sInfoFiltered": " 数据表中共为  _MAX_  条记录 ",
                "sSearch": " 搜索 ",
                "oPaginate": {
                    "sFirst": " 首页 ",
                    "sPrevious": " 上一页 ",
                    "sNext": " 下一页 ",
                    "sLast": " 末页 "
                }
            },
            "ajax": {
                "url": "/getOrders",
                "data": function (d) {
                    d.customerId = $('#customerId').val();
                    d.orderId = $('#orderId').val();

                },
                "type": "post"
            },
            columns: [
                {"data": "orderId"},
                {
                    "data": function (obj) {
                        let result;
                        switch (obj.orderState) {
                            case 0:
                                result = "已取消";
                                break;
                            case 1:
                                result = "租车待审核";
                                break;
                            case 2:
                                result = "出租中";
                                break;
                            case 4:
                                result = "已超时";
                                break;
                            case 5:
                                result = "还车待审核";
                                break;
                            case 7:
                                result = "一次审核完成(未付租金)";
                                break;
                            case 6:
                                result = "订单已完成";
                                break;
                            case 8:
                                result = "二次审核完成（未结算）";
                                break;
                            case 9:
                                result = "一次审核完成(已付租金)";
                                break;
                            case -1:
                                result = "订单异常";
                                break;
                        }
                        return result;
                    }
                },
                {"data": "orderCarId"},
                {"data": "customerId"},
                {
                    "data": function (obj) {
                        return getMyDate(obj.orderReserveStartDate)
                    }
                },
                {
                    "data": function (obj) {
                        if (new Date().getTime() - new Date(obj.orderReserveEndDate).getTime() - 86400000 > 0 && obj.orderState !== 0 && obj.orderState !== 4) {
                            $.ajax({
                                type: 'post',
                                url: '/updateOrderState',
                                data: {
                                    orderId: obj.orderId,
                                    orderState: 4,

                                },
                                success: function () {
                                    initTable()
                                }
                            })
                        }
                        return getMyDate(obj.orderReserveEndDate)
                    }
                },
                {
                    "data": function (obj) {
                        return obj.endDepartmentId;
                    }
                },
                {
                    "data": function (obj) {
                        return obj.endDepartmentId;
                    }
                },
                {
                    "data": function (obj) {
                        return getMyDateTime(obj.orderStartDate);
                    }
                },
                {
                    "data": function (obj) {
                        if (obj.orderState === 0) {
                            return "订单已取消"
                        }
                        if (obj.orderState === 4) {
                            return "订单已超时"
                        }
                        return getMyDateTime(obj.orderEndDate);
                    }
                }
            ],
            "columnDefs": [
                {
                    "targets": [10],
                    "data": "orderId",
                    "render": function (data, type, full) {
                        var YJZuJin = "<a href='javascript:;' id='YJZuJin' data-orderId='" + data + "' data-orderState='" + full.orderState + "' data-carId='" + full.orderCarId+ "' data-orderDeposit='" + full.orderDeposit + "'>支付押金</a>"

                        var ZFZuJin = "<a href='javascript:;' id='ZFZuJin' data-orderId='" + data + "' data-orderState='" + full.orderState + "' data-carId='" + full.orderCarId + "'>支付租金</a>"
                        var jiesuan = "<a href='javascript:;' id='jiesuan' data-orderId='" + data + "' data-orderState='" + full.orderState + "' data-carId='" + full.orderCarId + "'>结算</a>"

                        var check = "<a href='javascript:;' id='check' data-orderId='" + data + "' data-orderState='" + full.orderState + "' data-carId='" + full.orderCarId + "' data-orderDepositState='" + full.orderDepositState + "'>审核</a>"
                        var returnCar = "<a href='javascript:;' id='carReturn' data-orderId='" + data + "' data-orderState='" + full.orderState + "' data-carId='" + full.orderCarId + "'>还车</a>"
                        var more = "<a href='javascript:;' id='showMore' data-orderId='" + data + "' data-orderDeposit='" + full.orderDeposit + "' data-carId='" + full.orderCarId + "' data-orderDepositState='" + full.orderDepositState + "'>更多</a>"
                        if (full.orderState === 1 || full.orderState === 5) {
                            if(full.orderDepositState ===0){
                                return check + " " + more+" "+YJZuJin
                            }else{
                                return check + " " + more
                            }
                        }else if(full.orderState===4){
                            return check + " " + more
                        }
                        if (full.orderState === 4) {
                            return returnCar + " " + more;
                        }
                        else if (full.orderState === 2) {
                            return returnCar + " " + more;
                        } else if (full.orderState == 8) {
                            return jiesuan + " " + more;
                        } else if (full.orderState == 7) {
                            return ZFZuJin + " " + check + " " + more;
                        } else if (full.orderState == 9) {
                            return check + " " + more;
                        } else {
                            return more;
                        }
                    }
                },
                {
                    "targets": [6],
                    "data": "startDepartmentId",
                    "render": function (data, type, full) {
                        var dept = "<a title=\"门店信息\" href=\"javascript:;\" onclick=\"layer_show('门店信息','/toDeptInfo?deptId=" + data + "','300','200')\" class=\"ml-5\" style=\"text-decoration:none\">查看</a>";
                        return data + '---' + dept;
                    }
                },
                {
                    "targets": [7],
                    "data": "endDepartmentId",
                    "render": function (data, type, full) {
                        var dept = "<a title=\"门店信息\" href=\"javascript:;\" onclick=\"layer_show('门店信息','/toDeptInfo?deptId=" + data + "','300','200')\" class=\"ml-5\" style=\"text-decoration:none\">查看</a>";
                        return data + '---' + dept;
                    }
                },
                {
                    "targets": [3],
                    "data": "customerId",
                    "render": function (data, type, full) {
                        var customer = "<a title=\"客户信息\" href=\"javascript:;\" onclick=\"layer_show('客户信息','/toCustomerInfo?customerId=" + data + "','500','300')\" class=\"ml-5\" style=\"text-decoration:none\">查看</a>";
                        return data + '---' + customer;
                    }
                },
                {
                    "targets": [2],
                    "data": "orderCarId",
                    "render": function (data, type, full) {
                        var car = "<a title=\"客户信息\" href=\"javascript:;\" onclick=\"layer_show('汽车信息','/toCarInfo?carId=" + data + "','500','500')\" class=\"ml-5\" style=\"text-decoration:none\">查看</a>"
                        return data + '---' + car;
                    }
                }
            ],
        })
        ;
    }

    //支付租金
    $('table tbody').on('click', 'tr #YJZuJin', function () {
        var orderId = $(this).attr("data-orderId");
        var carId = $(this).attr("data-carId");
        var orderState = $(this).attr("data-orderState");
        var orderDeposit = $(this).attr("data-orderDeposit");

        if (orderState == 1) {
            layer.confirm("请确认使用哪种交易方式，需付款："+orderDeposit+"元",
                {btn: ["现金", "支付宝"],
                    btn1:function () {
                    typePayXJ(orderId)
                },
                    btn2:function () {
                    typepayZFB(orderId)
                }
                });
        }
    })

    function typePayXJ(orderId){
        layer.open({
            type: 2,
            title: "支付押金",
            fix: false,
            maxmin: true,
            shadeClose: true,
            area: ['1000px', '800px'],
            content: '/YJPriceYes?out_trade_no=' + orderId+'&type_DepositState=1',
            end: function () {
                initTable();
            }
        })
    }
    function typepayZFB(orderId){
        layer.open({
            type: 2,
            title: "支付押金",
            fix: false,
            maxmin: true,
            shadeClose: true,
            area: ['1000px', '800px'],
            content: '/ZhiFuYJ?orderId=' + orderId,
            end: function () {
                initTable();
            }
        })
    }

    //支付租金
    $('table tbody').on('click', 'tr #ZFZuJin', function () {
        var orderId = $(this).attr("data-orderId");
        var carId = $(this).attr("data-carId");
        var orderState = $(this).attr("data-orderState");
        if (orderState == 7) {
                layer.open({
                    type: 2,
                    title: "支付租金",
                    fix: false,
                    maxmin: true,
                    shadeClose: true,
                    area: ['1000px', '800px'],
                    content: '/jieSuanOrder?orderId=' + orderId,
                    end: function () {
                        initTable();
                    }
                })
        }
    })

    //结算
    $('table tbody').on('click', 'tr #jiesuan', function () {
        console.log(1);
        var orderId = $(this).attr("data-orderId");
        var carId = $(this).attr("data-carId");
        var orderState = $(this).attr("data-orderState");
        if (orderState == 8) {
                layer.open({
                    type: 2,
                    title: "结算",
                    fix: false,
                    maxmin: true,
                    shadeClose: true,
                    area: ['1000px', '800px'],
                    content: '/toCheckJIeSuan?orderId=' + orderId,
                    end: function () {
                        initTable();
                    }
                })
        }
    })

    //查看更多信息
    $('table tbody').on('click', 'tr #showMore', function () {
        const orderId = $(this).attr("data-orderId");
        const orderDeposit = $(this).attr("data-orderDeposit");
        const orderDepositState = $(this).attr("data-orderDepositState")
        const orderInfo = '订单号:<span id="id"></span><br>' +
            '押金:<span id="deposit"></span><br>' +
            '押金状态:<span id="state"></span><br>';
        layer.open({
            type: 1,
            title: '更多信息',
            area: ['250px', '200px'],
            shadeClose: true,
            content: orderInfo,
            btn: ['确定'],
            yes: function (index) {
                layer.close(index)
            },
            end: function () {
            }
        })
        $('#id').text(orderId)
        $('#deposit').text(orderDeposit + "￥");
        var str = '';
        if (orderDepositState == 0) {
            str = "未缴纳"
        } else if (orderDepositState == 1) {
            str = "已缴纳"
        } else {
            str = "已退还"
        }
        $('#state').text(str)
    })
    //审核
    $('table tbody').on('click', 'tr #check', function () {
        var orderId = $(this).attr("data-orderId");
        var carId = $(this).attr("data-carId");
        var orderState = $(this).attr("data-orderState")
        var orderDepositState = $(this).attr("data-orderDepositState")
        if (orderState == 5) {
            layer.open({
                type: 2,
                title: "还车审核",
                fix: false,
                maxmin: true,
                shadeClose: true,
                area: ['1000px', '800px'],
                content: '/tochecklist',
                end: function () {
                    initTable();
                }
            });
        } else if (orderState == 9) {
            layer.open({
                type: 2,
                title: "还车二次审核",
                fix: false,
                maxmin: true,
                shadeClose: true,
                area: ['1000px', '800px'],
                content: '/tochecklist',
                end: function () {
                    initTable();
                }
            });
        } else if (orderState == 7) {
            layer.alert("请先支付租金在进行审核")
        } else if (orderState == 1) {
            layer.confirm("确认审核通过吗", {
                btn: ["通过", "不通过"],
                cancel: function (index) {
                   layer.close(index)
                    //通过
                },btn1:function(index){
                    if (orderDepositState == 0) {
                        layer.alert("押金尚未支付！")
                        return;
                    }
                    $.ajax({
                        type: 'post',
                        url: '/updateOrderState',
                        data: {
                            orderId: orderId,
                            orderState: 2
                        },
                        success: function (data) {
                            $("#orderListTable").dataTable().fnClearTable();
                            layer.close(index)
                            initTable();
                            $("html, body").animate({
                                scrollTop: $('html, body').get(0).scrollHeight
                            }, 1000);
                        }
                    })
                    //不通过
                },btn2:function (index) {
                    $.ajax({
                        type: 'post',
                        url: '/updateOrderState',
                        data: {
                            orderId: orderId,
                            orderState: 0
                        },
                        success: function (data) {
                            $.ajax({
                                type: "post",
                                url: '/updateCarState',
                                data: {
                                    carId: carId,
                                    carState: 0
                                },
                                success: function (data) {
                                    $("#orderListTable").dataTable().fnClearTable();
                                    layer.close(index)
                                    initTable();
                                    $("html, body").animate({
                                        scrollTop: $('html, body').get(0).scrollHeight
                                    }, 1000);
                                }
                            })
                        }
                    })
                }

            })
        } else {
            layer.alert("该订单无需审核，请确认")
        }
    });
    //还车
    $('table tbody').on('click', 'tr #carReturn', function () {
        var carId = $(this).attr("data-carId");
        var orderId = $(this).attr("data-orderId");
        var orderState = $(this).attr("data-orderState");
        $.ajax({
            url: '/getCarByID',
            type: 'post',
            data: {
                carId: carId
            },
            success: function (data) {
                if (orderState == 2) {
                    layer.confirm("确认车已经归还了吗", {
                        btn: ["确认", "取消"]
                    }, function (index) {
                        layer.close(index)
                        var oil = '剩余油量:<input type="text" id="orderEndCarOilValue"/><br>';
                        var elec = '剩余电量:<input type="text" id="orderEndCarElectriclValue"/><br>';
                        var carInfo = '';
                        if (data.carDriverType == 0) {
                            carInfo = oil
                        } else if (data.carDriverType == 1) {
                            carInfo = elec
                        } else {
                            carInfo = oil + elec;
                        }
                        layer.open({
                            type: 1,
                            title: '输入归还时汽车信息',
                            area: ['400px'],
                            shadeClose: true,
                            content: carInfo,
                            btn: ['确定', '取消'],
                            yes: function (index) {
                                var orderEndCarOilValue = $('#orderEndCarOilValue').val();
                                var orderEndCarElectriclValue = $('#orderEndCarElectriclValue').val();
                                layer.close(index)
                                $.ajax({
                                    type: 'post',
                                    url: '/updateCarState',
                                    data: {
                                        carId: carId,
                                        carState: 0
                                    },
                                    success: function () {
                                        $.ajax({
                                            type: "post",
                                            url: '/updateOrderInfo',
                                            data: {
                                                orderId: orderId,
                                                orderState: 5,
                                                orderEndCarOilValue: orderEndCarOilValue,
                                                orderEndCarElectriclValue: orderEndCarElectriclValue,
                                            },
                                            success: function (data) {
                                                layer.alert("还车成功")
                                                $("#orderListTable").dataTable().fnClearTable();
                                                layer.close(index)
                                                initTable();
                                                $("html, body").animate({
                                                    scrollTop: $('html, body').get(0).scrollHeight
                                                }, 1000);

                                                $.ajax({
                                                    url: '/addcheck',
                                                    type: 'post',
                                                    data: {
                                                        carId: carId,
                                                        orderID: orderId
                                                    },
                                                    success: function (data) {
                                                        layer.alert(data.flag + ":" + data.msg)
                                                    }
                                                })
                                            }
                                        })

                                    }
                                })
                            }
                        })

                        $('#orderEndCarOilValue').val("0")
                        $('#orderEndCarElectriclValue').val("0")
                    }, function () {
                    })
                } else {
                    layer.alert("该订单车辆无需归还")
                }
            }
        })

    })


    $("#searchOrders").click(function () {
        $("#orderListTable").dataTable().fnClearTable();
        initTable();
    });


    function getMyDate(time) {
        if (typeof (time) == "undefined") {
            return "";
        }
        var oDate = new Date(time),
            oYear = oDate.getFullYear(),
            oMonth = oDate.getMonth() + 1,
            oDay = oDate.getDate(),
            oTime = oYear + '年' + getzf(oMonth) + '月' + getzf(oDay) + '日 ';
        return oTime;
    };

    function getMyDateTime(time) {
        if (typeof (time) == "undefined" || time === null) {
            return "订单还未结束";
        }

        var oDate = new Date(time),
            oYear = oDate.getFullYear(),
            oMonth = oDate.getMonth() + 1,
            oDay = oDate.getDate(),
            oHour = oDate.getHours(),
            oMin = oDate.getMinutes(),
            oSen = oDate.getSeconds(),
            oTime = oYear + '年' + getzf(oMonth) + '月' + getzf(oDay) + '日 ' + getzf(oHour) + ':' + getzf(oMin) + ':' + getzf(oSen);//最后拼接时间
        return oTime;
    };

    //补0操作,当时间数据小于10的时候，给该数据前面加一个0
    function getzf(num) {
        if (parseInt(num) < 10) {
            num = '0' + num;
        }
        return num;
    }


</script>
</body>
</html>